<style lang="scss">
.v-body {
    width: 750rpx;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    image {
        position: absolute;
        top: 100rpx;
        left: 14%;
        width: 72%;
    }
    button {
        position: absolute;
        bottom: 70rpx;
        left: 150rpx;
        width: 450rpx;
        height: 88rpx;
        line-height: 88rpx;
        border: 1px solid #ff6702;
        color: #ff6702;
        z-index: 999;
        background: #fff;
    }
    .v-ts {
        width: 750rpx;
        position: absolute;
        bottom: 225rpx;
        text-align: center;
        .v-h1 {
            font-size: 40rpx;
        }
        .v-h3 {
            font-size: 26rpx;
            color: #999999;
            margin-top: 28rpx;
        }
    }
}
</style>
<template>
    <view class="v-body" v-show="false">
        <view class="v-ts">
            <view class="v-h1">微信授权</view>
            <view class="v-h3">该页面为微信授权页，请点击下方按钮进行授权</view>
        </view>
        <button open-type="getUserInfo" class="v-goyz" @getuserinfo="onGotUserInfo">{{ issq ? '登录中...' : '点击微信授权' }}</button>
        <image src="/static/img/bglogin.png" mode="widthFix" />
    </view>
</template>
<script>
import { logina, loginb } from '../../api/api';
export default {
    data() {
        return {
            userInfo: '',
            encryptedData: '',
            iv: '',
            issq: false,
            signature: '',
            code: '',
            openid: '',
            sessionkey: '',
            type: ''
        };
    },
    methods: {
        async onGotUserInfo(e) {
            var that = this;
            if (e.detail.errMsg == 'getUserInfo:ok') {
                this.issq = true;
                console.log(e);
                wx.setStorageSync('userInfo', e.detail.userInfo);
                that.userInfo = e.detail.userInfo;
                that.encryptedData = e.detail.encryptedData;
                that.iv = e.detail.iv;
                that.signature = e.detail.signature;
                that.wxlogin();
            } else {
                this.issq = false;
                uni.showToast({
                    title: '授权失败',
                    icon: 'none'
                });
            }
        },
        async wxlogin() {
            let that = this;
            wx.login({
                success: res => {
                    console.log('wxlogin', res);
                    that.code = res.code;
                    that.logina();
                }
            });
        },
        async logina() {
            let res = await logina({
                code: this.code,
                loadingTip: '登录中'
            });

            this.openid = res.openid;
            this.$setOpenid(this.openid);
            this.sessionkey = res.session_key;

            if (this.type) {
                this.$setRole(this.type);
                this.loginb();
            } else {
                this.goChooseUser();
            }
            return res;
        },
        async loginb() {
            var that = this;
            loginb({
                openid: this.openid,
                type: this.type,
                // name: this.userInfo.nickName,
                // sex: this.userInfo.gender == 1 ? '男' : '女',
                // headimg: this.userInfo.avatarUrl,
                sessionkey: this.sessionkey
            }).then(res => {
                //console.log(res.data)
                wx.setStorageSync('userdetails', res.userinfo);
                this.$setIdentification(res.userinfo.identification);
                this.$setSessionKey(res.userinfo.sessionkey);
                let url = '';
                let type = that.type;
                if (type == 2) {
                    url = '..yz/yzhome?type=2';
                } else if (type == 1) {
                    url = '..ay/ayhome?type=1';
                } else if (type == 3) {
                    if (res.userinfo.identification == 0) {
                        uni.navigateTo({
                            url: 'index?type=' + type
                        });
                        return;
                    } else {
                        url = 'gs/gshome?type=' + type;
                    }
                } else {
                    console.log('no type, go choose user');
                    url = 'shooseuser';
                }
                uni.reLaunch({
                    url: url
                });
            });
        },
        goChooseUser() {
            uni.reLaunch({
                url: 'chooseuser'
            });
        }
    },
    onLoad(opt) {
        console.log('onload options', opt);
        var that = this;
        this.type = opt.type;

        if (opt.type) {
            this.$setRole(this.type);
        } else {
            this.type = this.$global.role;
        }
        console.log('loginpage role is ', this.type)
        uni.redirectTo({
            url: 'chooseuser'
        })
    }
};
</script>
